home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1996 / MacHack 1996.toast / Hacks / Hacks ’87 / Source ƒ.sit / Source ƒ / C ƒ / TRANS-LSC / DialogSkel.c next >
Encoding:
C/C++ Source or Header  |  1986-11-03  |  5.8 KB  |  373 lines  |  [TEXT/KAHL]

  1.  
  2. # include    <DialogMgr.h>
  3. # include    <EventMgr.h>
  4. # include    <MenuMgr.h>
  5. # include    <ToolBoxUtil.h>
  6. # include    <Pascal.h>
  7.  
  8.  
  9. # define    nil            (0L)
  10.  
  11. typedef enum
  12. {
  13.     mDlogRes = 1000,
  14.     aboutAlrtRes    /* About... alert resource number */
  15. };
  16.  
  17.  
  18. typedef enum                /* File menu item numbers */
  19. {
  20.     showDlog1 = 1,
  21.     showDlog2,
  22.     /* --- */
  23.     quit = 4
  24. };
  25.  
  26.  
  27. typedef enum                 /* Edit menu item numbers */
  28. {
  29.     undo = 1,
  30.     /* --- */
  31.     cut = 3,
  32.     copy,
  33.     paste,
  34.     clear
  35. };
  36.  
  37.  
  38. typedef enum                /* dialog item numbers */
  39. {
  40.     button1 = 1,
  41.     edit1,
  42.     static1,
  43.     radio1,
  44.     radio2,
  45.     radio3,
  46.     check1,
  47.     check2,
  48.     user1
  49. };
  50.  
  51.  
  52. DialogPtr    mDlog1;
  53. DialogPtr    mDlog2;
  54. int            iconNum1 = 0;
  55. int            iconNum2 = 0;
  56.  
  57. pascal void DrawIcon (dlog, item)
  58. DialogPtr    dlog;
  59. int            item;
  60. {
  61. Handle    h;
  62. Handle    itemHandle;
  63. int        itemType;
  64. Rect    itemRect;
  65.  
  66.     GetDItem (dlog, item, &itemType, &itemHandle, &itemRect);
  67.     h = GetIcon (dlog == mDlog1 ? iconNum1 : iconNum2);
  68.     PlotIcon (&itemRect, h);
  69. }
  70.  
  71.  
  72. Boolean GetDCtl (dlog, item)
  73. DialogPtr    dlog;
  74. int            item;
  75. {
  76. Handle    itemHandle;
  77. int        itemType;
  78. Rect    itemRect;
  79.  
  80.     GetDItem (dlog, item, &itemType, &itemHandle, &itemRect);
  81.     return (GetCtlValue (itemHandle));
  82. }
  83.  
  84.  
  85. SetDCtl (dlog, item, value)
  86. DialogPtr    dlog;
  87. int            item;
  88. Boolean        value;
  89. {
  90. Handle    itemHandle;
  91. int        itemType;
  92. Rect    itemRect;
  93.  
  94.     GetDItem (dlog, item, &itemType, &itemHandle, &itemRect);
  95.     SetCtlValue (itemHandle, (int) value);
  96. }
  97.  
  98.  
  99. GetDText (dlog, item, str)
  100. DialogPtr    dlog;
  101. int            item;
  102. StringPtr    str;
  103. {
  104. Handle    itemHandle;
  105. int        itemType;
  106. Rect    itemRect;
  107.  
  108.     GetDItem (dlog, item, &itemType, &itemHandle, &itemRect);
  109.     GetIText (itemHandle, str);
  110. }
  111.  
  112.  
  113. SetDText (dlog, item, str)
  114. DialogPtr    dlog;
  115. int            item;
  116. Str255        str;
  117. {
  118. Handle    itemHandle;
  119. int        itemType;
  120. Rect    itemRect;
  121.  
  122.     GetDItem (dlog, item, &itemType, &itemHandle, &itemRect);
  123.     SetIText (itemHandle, str);
  124. }
  125.  
  126.  
  127. SetDProc (dlog, item, p)
  128. DialogPtr    dlog;
  129. int            item;
  130. ProcPtr        p;
  131. {
  132. Handle    itemHandle;
  133. int        itemType;
  134. Rect    itemRect;
  135.  
  136.     GetDItem (dlog, item, &itemType, &itemHandle, &itemRect);
  137.     SetDItem (dlog, item, itemType, p, &itemRect);
  138. }
  139.  
  140.  
  141. SetDRadio (dlog, item)
  142. DialogPtr    dlog;
  143. int            item;
  144. {
  145. DialogPtr    partner;
  146. Handle        itemHandle;
  147. int            itemType;
  148. Rect        itemRect;
  149.  
  150.     partner = (DialogPtr) GetWRefCon (dlog);
  151.     SetDCtl (dlog, radio1, item == radio1);
  152.     SetDCtl (dlog, radio2, item == radio2);
  153.     SetDCtl (dlog, radio3, item == radio3);
  154.  
  155.     if (partner == mDlog1)
  156.         iconNum1 = item - radio1;
  157.     else
  158.         iconNum2 = item - radio1;
  159.  
  160.     GetDItem (partner, user1, &itemType, &itemHandle, &itemRect);
  161.     SetPort (partner);
  162.     InvalRect (&itemRect);
  163. }
  164.  
  165.  
  166. Event (item, event)
  167. int            item;
  168. EventRecord    *event;
  169. {
  170. DialogPtr    actor, partner;
  171. Str255        title;
  172. Boolean        value;
  173.  
  174.     GetPort (&actor);
  175.     partner = (DialogPtr) GetWRefCon (actor);
  176.     switch (item)
  177.     {
  178.         case button1:
  179.             GetDText (actor, edit1, title);
  180.             SetWTitle (partner, title);
  181.             break;
  182.  
  183.         /* set radio buttons */
  184.  
  185.         case radio1:
  186.             SetDRadio (actor, radio1);
  187.             break;
  188.  
  189.         case radio2:
  190.             SetDRadio (actor, radio2);
  191.             break;
  192.  
  193.         case radio3:
  194.             SetDRadio (actor, radio3);
  195.             break;
  196.  
  197.         /* flip check boxes */
  198.  
  199.         case check1:
  200.             value = !GetDCtl (actor, item);
  201.             SetDCtl (actor, item, value);
  202.             if (value == false)
  203.                 HideWindow (partner);
  204.             else
  205.                 ShowWindow (partner);
  206.             break;
  207.  
  208.         case check2:
  209.             value = !GetDCtl (actor, check2);
  210.             SetDCtl (actor, check2, value);
  211.             ((WindowPeek) partner)->goAwayFlag = (char) (value ? 255 : 0);
  212.             break;
  213.     }
  214. }
  215.  
  216.  
  217. Close ()
  218. {
  219. DialogPtr    actor, partner;
  220.  
  221.     GetPort (&actor);
  222.     partner = (DialogPtr) GetWRefCon (actor);
  223.     HideWindow (actor);
  224.     SetDCtl (partner, check1, false);
  225. }
  226.  
  227.  
  228. Clobber ()
  229. {
  230. DialogPtr    theDialog;
  231.  
  232.     GetPort (&theDialog);
  233.     DisposDialog (theDialog);
  234. }
  235.  
  236.  
  237. /*
  238.     File menu handler
  239. */
  240.  
  241. DoFileMenu (item)
  242. int        item;
  243. {
  244.  
  245.     switch (item)
  246.     {
  247.         case showDlog1:
  248.             SelectWindow (mDlog1);
  249.             ShowWindow (mDlog1);
  250.             SetDCtl (mDlog2, check1, true);
  251.             break;
  252.         
  253.         case showDlog2:
  254.             SelectWindow (mDlog2);
  255.             ShowWindow (mDlog2);
  256.             SetDCtl (mDlog1, check1, true);
  257.             break;
  258.     
  259.         case quit:
  260.             SkelWhoa ();
  261.             break;
  262.     }
  263. }
  264.  
  265.  
  266. /*
  267.     Handle Edit menu items for text window
  268. */
  269.  
  270. DoEditMenu (item)
  271. int        item;
  272. {
  273. DialogPtr    theDialog;
  274.  
  275.     theDialog = (DialogPtr) FrontWindow ();
  276.     if (((WindowPeek) theDialog)->windowKind != dialogKind)
  277.         return;
  278.  
  279.     switch (item)
  280.     {
  281.         case cut:
  282.         {
  283.             DlgCut (theDialog);
  284.             (void) ZeroScrap ();
  285.             (void) TEToScrap ();
  286.             break;
  287.         }
  288.  
  289.         case copy:
  290.         {
  291.             DlgCopy (theDialog);
  292.             (void) ZeroScrap ();
  293.             (void) TEToScrap ();
  294.             break;
  295.         }
  296.  
  297.         case paste:
  298.         {
  299.             (void) TEFromScrap ();
  300.             DlgPaste (theDialog);
  301.             break;
  302.         }
  303.  
  304.         case clear:
  305.         {
  306.             DlgDelete (theDialog);
  307.             break;
  308.         }
  309.     }
  310. }
  311.  
  312.  
  313. /*
  314.     Handle selection of About… item from Apple menu
  315. */
  316.  
  317. DoAbout ()
  318. {
  319.     (void) Alert (aboutAlrtRes, nil);
  320. }
  321.  
  322.  
  323. DialogPtr DemoDialog (title, x, y)
  324. StringPtr    title;
  325. int            x, y;
  326. {
  327. DialogPtr    theDialog;
  328.  
  329.     theDialog = GetNewDialog (mDlogRes, nil, -1L);
  330.     MoveWindow (theDialog, x, y, false);
  331.     SetWTitle (theDialog, title);
  332.     SkelDialog (theDialog, Event, Close, Clobber);
  333.     return (theDialog);
  334. }
  335.  
  336.  
  337. main ()
  338. {
  339. MenuHandle    m;
  340.  
  341.     SkelInit ();
  342.     SkelApple ("\pAbout DialogSkel…", DoAbout);
  343.  
  344.     m = NewMenu (1000, "\pFile");
  345.     AppendMenu (m, "\pShow Dialog 1;Show Dialog 2;(-");
  346.     AppendMenu (m, "\pQuit/Q");
  347.     SkelMenu (m, DoFileMenu, nil);
  348.  
  349.     m = NewMenu (1001, "\pEdit");
  350.     AppendMenu (m, "\p(Undo/Z;(-;Cut/X;Copy/C;Paste/V;Clear");
  351.     SkelMenu (m, DoEditMenu, nil);
  352.     
  353.     mDlog1 = DemoDialog ("\pModeless Dialog 1", 50, 50);
  354.     mDlog2 = DemoDialog ("\pModeless Dialog 2", 150, 200);
  355.     SetWRefCon (mDlog1, mDlog2);
  356.     SetWRefCon (mDlog2, mDlog1);
  357.     SetDText (mDlog1, edit1, "\pModeless Dialog 2");
  358.     SetDText (mDlog2, edit1, "\pModeless Dialog 1");
  359.     SetDProc (mDlog1, user1, DrawIcon);
  360.     SetDProc (mDlog2, user1, DrawIcon);
  361.     SetDCtl (mDlog1, radio1, true);
  362.     SetDCtl (mDlog2, radio1, true);
  363.     SetDCtl (mDlog1, check1, true);
  364.     SetDCtl (mDlog2, check1, true);
  365.     SetDCtl (mDlog1, check2, true);
  366.     SetDCtl (mDlog2, check2, true);
  367.     ShowWindow (mDlog1);
  368.     ShowWindow (mDlog2);
  369.  
  370.     SkelMain ();
  371.     SkelClobber ();
  372. }
  373.